package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bwie.domain.User;
import com.bwie.mapper.UserMapper;
import com.bwie.service.UserService;
import com.bwie.utils.R;
import com.bwie.vo.LoginInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author: Zuo
 * @Date: 2024/12/23 19:26
 * @Description:
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public R login(LoginInfoVo loginInfoVo) {
        User user = userMapper.selectOne(new QueryWrapper<User>().lambda().eq(User::getUserName, loginInfoVo.getUserName()));
        if (user == null) {
            return R.ERROR(500, "用户不存在");
        }
//        if (user.getUserPass().equals(loginInfoVo.getUserPass())){
//            return R.ERROR(500,"密码输入错误");
//        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", user.getUserId())
                .claim("userName",user.getUserName())
                .compact();
        String tokenKey = "token:"+user.getUserId();
        stringRedisTemplate.opsForValue().set(tokenKey,token,5, TimeUnit.MINUTES);
        user.setToken(token);

        return R.OK(user);
    }
}
